﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Grizzy_X
{
    public class CPointLayer : CLayer
    {
        public List<CPoint> Point_Set;
        public Bitmap ptlayer;

        public bool NewPointLayer(int id,string name,string path,string des)
        {
            try
            {
                ID = id;
                Name = name;
                Path = path;
                Layer_Type = LayerType.CPointLayer;
                bSelected = true;
                bVisible = true;
                Point_Set = new List<CPoint>();
                Description = des;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddPoint(CPoint NewPoint)
        {
            try
            {
                Point_Set.Add(NewPoint);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DeletePoint()
        {
            try
            {
                for (int i = 0; i < Controller.Selected.Count(); i++)
                {
                    Point_Set.RemoveAt(Controller.Selected[i]);
                    Point_Set.Insert(Controller.Selected[i], new CPoint());
                    Point_Set[Controller.Selected[i]].points = new List<PointF>();
                }
                for (int i = 0; i <Point_Set.Count(); i++)
                {
                    if (Point_Set[i].points.Count() == 0)
                    {
                        Point_Set.RemoveAt(i);
                        i--;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //实现方法有待考虑
        public bool Union()
        {
            CPoint temppt = new CPoint();
            int id=Point_Set[Controller.Selected[0]].GetID();
            temppt.newCpoint(id, Point_Set[Controller.Selected[0]].LegendID);
            for (int i = 0; i < Controller.Selected.Count(); i++)
            {
                for (int j = 0; j < Point_Set[Controller.Selected[i]].points.Count(); j++)
                {
                    temppt.points.Add(Point_Set[Controller.Selected[i]].points[j]);
                }
            }
            temppt.updateMBR();
            DeletePoint();
            Point_Set.Add(temppt);
            Controller.Selected = new List<int>();
            Controller.Selected.Add(Point_Set.Count() - 1);
            return true;
        }

        public bool unUnion()
        {
            try
            {
                for (int i = 0; i <Point_Set[Controller.Selected[0]].points.Count();i++)
                {
                    CPoint temp = new CPoint();
                    //完善生成ID的步骤、重新生成外包矩形等操作，调用一些函数。。。
                    temp.newCpoint(IDGenerator.PointID(), Point_Set[Controller.Selected[0]].LegendID);
                    temp.points.Add(Point_Set[Controller.Selected[0]].points[i]);
                    temp.updateMBR();
                    Point_Set.Add(temp);
                }
                Point_Set.RemoveAt(Controller.Selected[0]);
                Controller.Selected = new List<int>();
                Controller.Selected.Add(Point_Set.Count() - 1);
                return true;
            }
            catch
            {
                return false;    
            }
        }

        public Bitmap drawpoint()
        {
            ptlayer=drawnoselect();
            drawselect();
            ptlayer=drawselectbounds(ptlayer);
            if (Controller.selectsimple != null)
            {
                if (Controller.selectsimple.Count() > 0)
                    ptlayer = drawsimplepoint(ptlayer);
            }
            return ptlayer;
        }

        public Bitmap selectlegend(CPointLegend legend,bool issimple)
        {
            Bitmap preview = new Bitmap((int)legend.size + 2, (int)legend.size + 2);//画笔默认为两个像素的宽度
            Graphics gra = Graphics.FromImage(preview);
            Pen pen;
            if (legend.color != Color.SkyBlue)
                pen = new Pen(Color.SkyBlue, 2);
            else
                pen = new Pen(Color.Blue, 2);
            if (issimple)
                pen.Color = Color.Red;
            Point[] points = new Point[4];
            points[0].X = (int)(0.5 * legend.size); points[0].Y = (int)(legend.size - Math.Sqrt(3) * 0.5 * legend.size);
            points[1].X = (int)(legend.size); points[1].Y = (int)(legend.size);
            points[2].X = 0; points[2].Y = (int)legend.size;
            points[3] = points[0];
            if (legend.Point_Style == CPointLegend.PointStyle.circle || legend.Point_Style == CPointLegend.PointStyle.filled_circle)
                gra.DrawEllipse(pen, 0, 0, legend.size, legend.size);
            else if (legend.Point_Style == CPointLegend.PointStyle.rectangle || legend.Point_Style == CPointLegend.PointStyle.filled_rectangle)
                gra.DrawRectangle(pen, 0, 0, legend.size, legend.size);
            else if (legend.Point_Style == CPointLegend.PointStyle.triangle || legend.Point_Style == CPointLegend.PointStyle.filled_triangle)
                gra.DrawLines(pen, points);
            else
                gra.DrawEllipse(pen, 0, 0, legend.size, legend.size);
            return preview;
        }
        public void SelectPoint(PointF e)
        {
            for (int i = 0; i < Point_Set.Count(); i++)
            {
                float trans;
                if (Controller.MyMap.Unit == "英寸")
                    trans = (float)1 / 96;
                else
                    trans = (float)2.54 / 96;
                float Buffer = Math.Max(((CPointLegend)Controller.MapLegends[Point_Set[i].LegendID]).size*trans / 2, 10*trans);
                if (Point_Set[i].MinBR.Lx - Buffer < e.X &&
                    Point_Set[i].MinBR.Ly - Buffer < e.Y &&
                   Point_Set[i].MinBR.Rx + Buffer > e.X &&
                    Point_Set[i].MinBR.Ry + Buffer > e.Y)
                {
                    for (int j = 0; j < (Point_Set[i].points.Count()); j++)
                    {
                        if (Math.Abs(Point_Set[i].points[j].X - e.X) < Buffer
                            && Math.Abs(Point_Set[i].points[j].Y - e.Y) < Buffer)
                        {
                            for (int m = 0; m < Controller.Selected.Count(); m++)
                            {
                                if (i ==Controller.Selected[m])
                                {
                                    Point_Set[i].isselected = false;
                                    Controller.Selected.RemoveAt(m);
                                    return;
                                }
                            }
                            Controller.SelectedObjectType = "Point";
                            Controller.Selected.Add(i);
                            //Controller.Selected.Add(i);
                            Point_Set[i].isselected = true;
                            return;
                        }
                    }
                }
            }
        }
        public Bitmap drawnoselect()
        {
            Bitmap bmp_new = new Bitmap(Controller.View_Width, Controller.View_Height);
            Graphics a = Graphics.FromImage(bmp_new);
            CPointLegend cpl = new CPointLegend();
            bool sign=true;
            for (int i = 0; i < Point_Set.Count(); i++)
            {
                for (int m = 0; m < Controller.Selected.Count(); m++)
                {
                    if (i == Controller.Selected[m])
                        sign = false;
                }
                if (sign)
                {
                    for (int j = 0; j < Point_Set[i].points.Count(); j++)
                    {
                        cpl = (CPointLegend)Controller.MapLegends[Point_Set[i].LegendID];
                        Point screen = new Point();
                        screen = Transformer.ToScreen(Point_Set[i].points[j]);
                        a.DrawImage(cpl.cpointlegend, screen.X - cpl.cpointlegend.Width / 2, screen.Y - cpl.cpointlegend.Height / 2, cpl.cpointlegend.Width, cpl.cpointlegend.Height);
                    }
                }
                sign = true;
            }
            return bmp_new;
        }
        public void drawselect()
        {
            Graphics a = Graphics.FromImage(ptlayer);
            CPointLegend cpl = new CPointLegend();
            for (int i = 0; i < Controller.Selected.Count(); i++)
            {
                cpl = (CPointLegend)Controller.MapLegends[Point_Set[Controller.Selected[i]].LegendID];
                for (int m = 0; m < Point_Set[Controller.Selected[i]].points.Count(); m++)
                {
                    Point screen = new Point();
                    screen = Transformer.ToScreen(Point_Set[Controller.Selected[i]].points[m]);
                    a.DrawImage(cpl.cpointlegend, screen.X - cpl.cpointlegend.Width / 2, screen.Y - cpl.cpointlegend.Height / 2, cpl.cpointlegend.Width, cpl.cpointlegend.Height);
                }
            }
        }
        public Bitmap drawselectbounds(Bitmap currentbmp)
        {
            //Bitmap bmp_new = ptlayer;
        
                Graphics a = Graphics.FromImage(currentbmp);
                if (Controller.selectsimple != null)
                {
                    if (Controller.selectsimple.Count() > 0)
                    {
                        bool sign = true;
                        CPointLegend cpl = new CPointLegend();
                        cpl = (CPointLegend)Controller.MapLegends[Point_Set[Controller.Selected[0]].LegendID];
                        for (int i = 0; i <Point_Set[Controller.Selected[0]].points.Count(); i++)
                        {
                            for(int j=0;j<Controller.selectsimple.Count();j++)
                            {
                                if(i==Controller.selectsimple[j])
                                    sign=false;
                            }
                            if(sign)
                            {
                                Point screen = new Point();
                                screen = Transformer.ToScreen(Point_Set[Controller.Selected[0]].points[i]);
                                a.DrawImage(cpl.cpointlegend, screen.X - cpl.cpointlegend.Width / 2, screen.Y - cpl.cpointlegend.Height / 2, cpl.cpointlegend.Width, cpl.cpointlegend.Height);
                            }
                            sign=true;
                        }
                    }
                }

                bool simple = false;
                for (int i = 0; i < Controller.Selected.Count(); i++)
                {
                    Bitmap drawselect = selectlegend((CPointLegend)Controller.MapLegends[Point_Set[Controller.Selected[i]].LegendID],simple);
                    for (int m = 0; m < Point_Set[Controller.Selected[i]].points.Count(); m++)
                    {
                        Point screen = new Point();
                        screen = Transformer.ToScreen(Point_Set[Controller.Selected[i]].points[m]);
                        a.DrawImage(drawselect, screen.X - drawselect.Width / 2, screen.Y - drawselect.Height / 2, drawselect.Width, drawselect.Height);
                    }
                }
                if (Controller.selectsimple != null)
                {
                    if (Controller.selectsimple.Count() > 0)
                    {
                        simple = true;
                        Bitmap drawselect = selectlegend((CPointLegend)Controller.MapLegends[Point_Set[Controller.Selected[0]].LegendID], simple);
                        for (int m = 0; m < Controller.selectsimple.Count(); m++)
                        {
                            Point screen = new Point();
                            screen = Transformer.ToScreen(Point_Set[Controller.Selected[0]].points[Controller.selectsimple[m]]);
                            a.DrawImage(drawselect, screen.X - drawselect.Width / 2, screen.Y - drawselect.Height / 2, drawselect.Width, drawselect.Height);
                        }
                    }
                }
                return currentbmp;
        }
        public Bitmap drawsimplepoint(Bitmap currentbmp)
        {
            Graphics a = Graphics.FromImage(currentbmp);
            CPointLegend cpl = new CPointLegend();
            cpl = (CPointLegend)Controller.MapLegends[Point_Set[Controller.Selected[0]].LegendID];
            bool simple = true;
            Bitmap drawselect = selectlegend((CPointLegend)Controller.MapLegends[Point_Set[Controller.Selected[0]].LegendID], simple);
            for (int i = 0; i < Controller.selectsimple.Count(); i++)
            {
                Point screen = new Point();
                screen = Transformer.ToScreen(Point_Set[Controller.Selected[0]].points[Controller.selectsimple[i]]);
                a.DrawImage(cpl.cpointlegend, screen.X - cpl.cpointlegend.Width / 2, screen.Y - cpl.cpointlegend.Height / 2, cpl.cpointlegend.Width, cpl.cpointlegend.Height);
                a.DrawImage(drawselect, screen.X - drawselect.Width / 2, screen.Y - drawselect.Height / 2, drawselect.Width, drawselect.Height);
            }
            return currentbmp;
        }
    }
}
